package com.example.ucs_alg_node_manager.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class ReturnVO<T> implements Serializable {
    private Integer code; //状态码
    private String message; //返回信息
    private T data; //数据

    //私有化
    private ReturnVO(){}

    //封装返回的数据
    public static <T> ReturnVO<T> build(T body, ResultCodeEnum resultCodeEnum){
        ReturnVO<T> returnVO = new ReturnVO<>();
        //封装数据
        if(body != null){
            returnVO.setData(body);
        }
        //状态码
        returnVO.setCode(resultCodeEnum.getCode());
        //相关返回数据
        returnVO.setMessage(resultCodeEnum.getMessage());
        return returnVO;
    }

    //成功
    //成功但里面没有数据
    public static<T> ReturnVO<T> ok(){
        return build(null, ResultCodeEnum.SUCCESS);
    }
    //成功里面有数据
    public static <T> ReturnVO<T> ok(T data){
        return build(data, ResultCodeEnum.SUCCESS);
    }
    public static<T> ReturnVO<T> ok(T data, ResultCodeEnum resultCodeEnum){
        return build(data,resultCodeEnum);
    }

    //失败
    //失败里面没数据
    public static<T> ReturnVO<T> fail(){
        return build(null, ResultCodeEnum.FAIL);
    }
    //失败里面有数据
    public static<T> ReturnVO<T> fail(T data){
        return build(data, ResultCodeEnum.FAIL);
    }

    public static<T> ReturnVO<T> fail(Integer code, String message){
        ReturnVO<T> returnVO = new ReturnVO<>();
        returnVO.setCode(code);
        returnVO.setMessage(message);
        returnVO.setData(null);
        return returnVO;
    }
    public static<T> ReturnVO<T> fail(T data, ResultCodeEnum dataError) {
        return build(data,dataError);
    }

    public ReturnVO<T> message(String msg){
        this.setMessage(msg);
        return this;
    }

    public ReturnVO<T> code(Integer code){
        this.setCode(code);
        return this;
    }

}
